package cn.wen.yaling.xo.service.impl;

import cn.wen.yaling.commons.enums.BlackListStatus;
import cn.wen.yaling.commons.service.RedisService;
import cn.wen.yaling.commons.utils.ResultEntity;
import cn.wen.yaling.commons.vo.BlackListAddVO;
import cn.wen.yaling.commons.vo.BlackListUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wen.yaling.commons.utils.PageUtils;
import cn.wen.yaling.commons.utils.Query;

import cn.wen.yaling.xo.dao.BlackListDao;
import cn.wen.yaling.commons.entity.BlackListEntity;
import cn.wen.yaling.xo.service.BlackListService;

import javax.annotation.Resource;


@Service("blackListService")
public class BlackListServiceImpl extends ServiceImpl<BlackListDao, BlackListEntity> implements BlackListService {

    @Autowired
    private BlackListDao blackListDao;

    @Resource
    private RedisService redisService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BlackListEntity> page = this.page(
                new Query<BlackListEntity>().getPage(params),
                new QueryWrapper<BlackListEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public BlackListEntity getByIP(String ip) {
        QueryWrapper<BlackListEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("black_ip", ip);
        return blackListDao.selectOne(queryWrapper);
    }

    @Override
    public ResultEntity getBlackList() {
        QueryWrapper<BlackListEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("black_status", BlackListStatus.NORMAL_USE.getStatus());
        List<BlackListEntity> list = blackListDao.selectList(queryWrapper);
        return ResultEntity.ok().put("data", list);
    }

    @Override
    public ResultEntity searchBlackList(String blackIp) {
        QueryWrapper<BlackListEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("black_status", BlackListStatus.NORMAL_USE.getStatus());
        queryWrapper.like("black_ip", blackIp);
        List<BlackListEntity> list = blackListDao.selectList(queryWrapper);
        return ResultEntity.ok().put("data", list);
    }

    @Override
    public ResultEntity addBlack(BlackListAddVO black) {
        BlackListEntity target = new BlackListEntity();
        BeanUtils.copyProperties(black, target);
        target.setBlackTimes(1);
        target.setCreateTime(new Date());
        target.setUpdateTime(new Date());
        target.setBlackStatus(BlackListStatus.NORMAL_USE.getStatus());
        boolean res = blackListDao.insert(target) > 0;
        // 添加到缓存中
        StringBuilder sb = new StringBuilder();
        sb.append("BLOG_BLACK_LIST:").append(black.getBlackIp());
        String blackListKey = sb.toString().replaceAll("/", ".");
        redisService.set(blackListKey, black.getBlackIp());
        if (res) return ResultEntity.ok();
        return ResultEntity.error();
    }

    @Override
    public ResultEntity deleteBlackByIds(List<Integer> blackIds) {
        boolean res = blackListDao.updateStatusByIds(blackIds, BlackListStatus.DELETED.getStatus());
        // 删除缓存
        List<BlackListEntity> blackListEntities = blackListDao.selectBatchIds(blackIds);
        for (BlackListEntity target : blackListEntities) {
            StringBuilder sb = new StringBuilder();
            sb.append("BLOG_BLACK_LIST:").append(target.getBlackIp());
            String blackListKey = sb.toString().replaceAll("/", ".");
            redisService.del(blackListKey + ":*");
        }
        if (res) return ResultEntity.ok();
        return ResultEntity.error();
    }

    @Override
    public ResultEntity updateBlackById(BlackListUpdateVO black) {
        BlackListEntity target = new BlackListEntity();
        BeanUtils.copyProperties(black, target);
        target.setUpdateTime(new Date());
        boolean res = blackListDao.updateById(target) > 0;
        if (res) return ResultEntity.ok();
        return ResultEntity.error();
    }

}